package com.weita.lightfood.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 文件上传工具
 * @author IT_Monster at 2019/05/21 09:55.
 */
public class FileUploadUtil {
    public static boolean uploadImg(MultipartFile file, String path, String fileName){
        if (file == null){
            return false;
        }
        System.out.println(file);
        BufferedOutputStream out = null;
        try {
            File folder = new File(path);
            if (!folder.exists()){
                folder.mkdirs();
            }
            out = new BufferedOutputStream(new FileOutputStream(path + fileName));
            if (out == null){
                return false;
            }
            // 写入新文件
            out.write(file.getBytes());
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }
}
